//package org.example.studentsystem_back.config;
//
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
//import org.springframework.core.annotation.Order;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.stereotype.Component;
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * ClassName: CorsFilter
// * Description:
// *
// * @author 肖鸿亮
// * @since JDK 1.8
// */
//@Component
//@WebFilter(urlPatterns = "/*")
//@Order(-99999)
//public class CorsFilter extends HttpFilter {
//    private static final long serialVersionUID = 2386571986045107652L;
//    private static final String OPTIONS_METHOD = "OPTIONS";
//
//    @Override
//    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
//        String origin = req.getHeader(HttpHeaders.ORIGIN);
//        System.out.println(origin);
//        if (!StringUtils.isBlank(origin)) {
//            // 允许客户端的域
//            res.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
//            // 允许客户端提交的Header
//            String requestHeaders = req.getHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
//            if (!StringUtils.isBlank(requestHeaders)) {
//                res.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders);
//            }
//            // 允许客户端访问的Header
//            res.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma, Token");
//            // 允许客户端携带凭证信息
//            res.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
//            // 允许客户端请求方法
//            res.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, OPTIONS, DELETE");
//            if (OPTIONS_METHOD.equalsIgnoreCase(req.getMethod())) {
//                res.setStatus(HttpServletResponse.SC_NO_CONTENT);
//                res.setContentType(MediaType.TEXT_HTML_VALUE);
//                res.setCharacterEncoding("utf-8");
//                res.setContentLength(0);
//                res.addHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "1800");
//                return;
//            }
//        }
//
//        super.doFilter(req, res, chain);
//    }
//}
package org.example.comp_algorithm_back.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class GlobalCorsConfig {
    /*
     * 允许跨域调用的过滤器
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 设置你要允许的网站域名
        config.addAllowedOrigin("http://172.26.6.132:8080");
        config.addAllowedOrigin("http://localhost:8081");
        config.addAllowedOrigin("http://localhost:8000");
        config.addAllowedOrigin("http://172.26.6.132");
        config.addAllowedOrigin("http://localhost:5173");
        config.addAllowedOrigin("http://localhost:5174");

        //允许跨域发送cookie
        config.setAllowCredentials(true);
        //放行全部原始头信息
        config.addAllowedHeader("*");
        //允许所有请求方法跨域调用
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
